home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / WINDOWS / SSHEET.ARJ / GENERIC.C next >
C/C++ Source or Header  |  1992-04-04  |  13KB  |  383 lines

  1. /*
  2.  *    This program is an introduction to using the Spread Sheet custom
  3.  *    control DLL.
  4.  *
  5.  * Both this program and the demo custom control DLL are shareware. You
  6.  * may copy and distribute them both freely. However, if you intend to
  7.  * use either of them as part of an application you'll need to register
  8.  * them. To register:
  9.  *
  10.  *                1. Fill out the registeration form and fax it to us
  11.  *                   The fax number is: 714-937-5830
  12.  *                or
  13.  *                2. Write to us through CompuServ, Id: 72520,2325
  14.  *
  15.  * You can buy the complete DLL or the full source for $29,99 and $49.99
  16.  * respectively. To order, send check or money order to
  17.  *
  18.  *
  19.  *       A. SHADMAN CO., P.O. BOX 28494, SANTA ANA, CA 92799-8494
  20.  *
  21.  *
  22.  * Legal notice:
  23.  *
  24.  *     We are granting a license to you to execute this program and the
  25.  *        accomponying DLL for evaluation purposes only.
  26.  *
  27.  *        WE MAKE NO WARRANTY OR REPRESENTATION, EITHER EXPRESS OR IMPLIED
  28.  *        WITH RESPECT TO THIS AND THE CUSTOM CONTROL DLL, INCLUDING THE
  29.  *        QUALITY, PERFORMANCE, MERCHANTIBILITY OR FITNESS FOR A PARTICULAR
  30.  *        PURPOSE.
  31.  *
  32.  *        Becuase of the complex nature of computer software this program or
  33.  *        the custom control DLL may not be completely free of errors.
  34.  *        You agree to take full responsibility for verification of any work
  35.  *        that includes either this program or the custom control DLL.
  36.  *        IN NO EVENT WILL A. SHADMAN CO. BE RESPONSIBLE FOR DIRECT, INDIRECT,
  37.  *        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  38.  *        USE OR INABILITY TO USE THIS PROGRAM OR THE CUSTOM CONTROL DLL.
  39.  *
  40.  * This program is based on the GENERIC program included with the
  41.  * Microsoft Windows SDK.
  42.  */
  43.  
  44. #include <windows.h>
  45. #include <string.h>
  46. #include "generic.h"
  47. #include "sshthdr.h"
  48.  
  49. BOOL InitSSheetData(VOID) ;
  50. VOID CreateDummySSheetData(HWND) ;
  51.  
  52. HANDLE hInst;                /* current instance                 */
  53. HWND hWndSSht, hWndEdt, hWndButt ;
  54.  
  55.  
  56. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  57. HANDLE hInstance;                 /* current instance         */
  58. HANDLE hPrevInstance;                 /* previous instance         */
  59. LPSTR lpCmdLine;                 /* command line             */
  60. int nCmdShow;                     /* show-window type (open/icon) */
  61. {
  62.      MSG msg;                     /* message                 */
  63.  
  64.      if (!hPrevInstance)             /* Other instances of app running? */
  65.     if (!InitApplication(hInstance)) /* Initialize shared things */
  66.          return (FALSE);         /* Exits if unable to initialize     */
  67.  
  68.      /* Perform initializations that apply to a specific instance */
  69.  
  70.      if (!InitInstance(hInstance, nCmdShow))
  71.           return (FALSE);
  72.  
  73.      /* Acquire and dispatch messages until a WM_QUIT message is received. */
  74.  
  75.      while (GetMessage(&msg,       /* message structure                 */
  76.         NULL,           /* handle of window receiving the message */
  77.         NULL,           /* lowest message to examine             */
  78.         NULL))           /* highest message to examine         */
  79.     {
  80.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  81.     DispatchMessage(&msg);       /* Dispatches message to window         */
  82.      }
  83.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  84. }
  85.  
  86.  
  87.  
  88. BOOL InitApplication(hInstance)
  89. HANDLE hInstance;                   /* current instance         */
  90. {
  91.     WNDCLASS  wc;
  92.  
  93.     /* Fill in window class structure with parameters that describe the       */
  94.      /* main window.                                                           */
  95.  
  96.      wc.style = NULL;                    /* Class style(s).                    */
  97.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  98.                                                      /* windows of this class.             */
  99.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  100.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  101.      wc.hInstance = hInstance;           /* Application that owns the class.   */
  102.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  103.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  104.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  105.     wc.lpszMenuName =  "GenericMenu";   /* Name of menu resource in .RC file. */
  106.     wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
  107.  
  108.     /* Register the window class and return success/failure code. */
  109.  
  110.     return (RegisterClass(&wc));
  111.  
  112. }
  113.  
  114.  
  115. BOOL InitInstance(hInstance, nCmdShow)
  116.      HANDLE          hInstance;          /* Current instance identifier.       */
  117.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  118. {
  119.     HWND            hWnd;               /* Main window handle.                */
  120.  
  121.     /* Save the instance handle in static variable, which will be used in  */
  122.     /* many subsequence calls from this application to Windows.            */
  123.  
  124.      hInst = hInstance;
  125.  
  126.      /* Create a main window for this application instance.  */
  127.  
  128.      hWnd = CreateWindow(
  129.           "GenericWClass",                /* See RegisterClass() call.          */
  130.           "SSHEET Sample Application",   /* Text for window title bar.         */
  131.           WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  132.           CW_USEDEFAULT,                  /* Default horizontal position.       */
  133.           CW_USEDEFAULT,                  /* Default vertical position.         */
  134.           CW_USEDEFAULT,                  /* Default width.                     */
  135.           CW_USEDEFAULT,                  /* Default height.                    */
  136.           NULL,                           /* Overlapped windows have no parent. */
  137.           NULL,                           /* Use the window class menu.         */
  138.           hInstance,                      /* This instance owns this window.    */
  139.           NULL                            /* Pointer not needed.                */
  140.      );
  141.  
  142.     /* If window could not be created, return "failure" */
  143.  
  144.     if (!hWnd)
  145.           return (FALSE);
  146.  
  147.     /* Make the window visible; update its client area; and return "success" */
  148.  
  149.      ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  150.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  151.      return (TRUE);               /* Returns the value from PostQuitMessage */
  152.  
  153. }
  154.  
  155.  
  156.  
  157. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  158. HWND hWnd;                  /* window handle             */
  159. unsigned message;              /* type of message             */
  160. WORD wParam;                  /* additional information         */
  161. LONG lParam;                  /* additional information         */
  162. {
  163.      FARPROC                 lpProcAbout;          /* pointer to the "About" function */
  164.  
  165.      static                     HANDLE hLib ;
  166.      static                     GLOBALHANDLE hSSheetData ;
  167.      SSHTCREATESTRUCT     SSheetData ;
  168.      char                        buffer[255] ;
  169.  
  170.      switch (message) {
  171.     case WM_CREATE:
  172.         //
  173.         //        Load the Custom Control Library.
  174.         //        Loading the library registers the controls class: TABL CTRL.
  175.         //
  176.         hLib = LoadLibrary("SSHTCTRL.DLL") ;
  177.         if (hLib < 32)
  178.             return (0) ;
  179.  
  180.       //        The control's id string must be copied into
  181.       //        the create structure.
  182.  
  183.       strcpy(SSheetData.szID, SSHEETID) ;
  184.       SSheetData.nRows        = 10 ;    // In demo version limited to 10
  185.         SSheetData.nCols        = 7 ;       // In demo version limited to 7
  186.         SSheetData.nColAnchor   = 0 ;
  187.         SSheetData.nRowAnchor   = 0 ;
  188.       SSheetData.nCurrentCol  = 0 ;
  189.         SSheetData.nCurrentRow  = 0 ;
  190.  
  191.         hWndSSht = CreateWindow(
  192.           CLASS_SSHEET,
  193.           NULL,
  194.           WS_CHILD | WS_VISIBLE | WS_BORDER,
  195.           50,
  196.           50,
  197.           400,
  198.           200,
  199.           hWnd,
  200.           1000,
  201.           hInst,
  202.             (LPSTR)&SSheetData);
  203.  
  204.  
  205.         // customize SSheet with headings, cell alignment etc.
  206.         if (!InitSSheetData())
  207.             return (FALSE) ;
  208.  
  209.          // put some data in SSheet
  210.         CreateDummySSheetData(hWndSSht) ;
  211.  
  212.         hWndEdt = CreateWindow(
  213.           "EDIT",
  214.           NULL,
  215.           WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
  216.           10,
  217.           10,
  218.           200,
  219.           30,
  220.           hWnd,
  221.           2000,
  222.           hInst,
  223.           NULL);
  224.  
  225.         hWndButt = CreateWindow(
  226.           "BUTTON",
  227.           NULL,
  228.           WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
  229.           220,
  230.           10,
  231.           120,
  232.           30,
  233.           hWnd,
  234.           3000,
  235.           hInst,
  236.           NULL);
  237.         SetWindowText(hWndButt,"Set Cell Value") ;
  238.  
  239.      return (0) ;
  240.  
  241.     case WM_RBUTTONDOWN:
  242.         SendMessage(hWndSSht,
  243.                         SS_SETCELLDATA, MAKEROWCOL(3,0), (LONG)"HI!") ;
  244.         SendMessage(hWndSSht, SS_DISPLAYCELL, MAKEROWCOL(3,0), NULL) ;
  245.         return (0) ;
  246.  
  247.     case WM_COMMAND:       /* message: command from application menu */
  248.         switch (wParam)
  249.             {
  250.             case IDM_ABOUT:
  251.                 lpProcAbout = MakeProcInstance(About, hInst);
  252.                 DialogBox(hInst,         /* current instance         */
  253.                      "AboutBox",             /* resource to use         */
  254.                      hWnd,             /* parent handle         */
  255.                      lpProcAbout);         /* About() instance address */
  256.                 FreeProcInstance(lpProcAbout);
  257.                 return (0) ;
  258.  
  259.             case 3000:    // button
  260.                 {
  261.                 int                nRow, nCol ;
  262.                 char                buffer[255] ;
  263.  
  264.                 nRow = SendMessage(hWndSSht, SS_GETCURROW, NULL, NULL) ;
  265.                 nCol = SendMessage(hWndSSht, SS_GETCURCOL, NULL, NULL) ;
  266.                 memset(buffer,'\0', sizeof(buffer)) ;
  267.                 GetWindowText(hWndEdt, buffer, 254) ;
  268.                 SendMessage(hWndSSht,
  269.                                 SS_SETCELLDATA, MAKEROWCOL(nRow,nCol),
  270.                                 (LONG)buffer) ;
  271.  
  272.                 SendMessage(hWndSSht, SS_DISPLAYCELL, MAKEROWCOL(nRow,nCol),
  273.                                 NULL) ;
  274.                 return (0) ;
  275.                 }
  276.  
  277.             default:
  278.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  279.             }
  280.  
  281.     case WM_DESTROY:          /* message: window being destroyed */
  282.          FreeLibrary(hLib) ;
  283.          if (hSSheetData)
  284.             GlobalFree(hSSheetData) ;
  285.          PostQuitMessage(0);
  286.          break;
  287.  
  288.     // SSHEET sends messages to its parent
  289.     case SSN_NEWCELL:
  290.         {
  291.         int                nRow, nCol ;
  292.  
  293.         nRow = SendMessage(hWndSSht, SS_GETCURROW, NULL, NULL) ;
  294.         nCol = SendMessage(hWndSSht, SS_GETCURCOL, NULL, NULL) ;
  295.         memset(buffer,'\0',sizeof(buffer)) ;
  296.         SendMessage(hWndSSht, SS_GETCELLDATA, MAKEROWCOL(nRow,nCol),
  297.                         (LONG)buffer) ;
  298.         SetWindowText(hWndEdt,buffer) ;
  299.         return (0) ;
  300.         }
  301.  
  302.  
  303.     default:              /* Passes it on if unproccessed    */
  304.          return (DefWindowProc(hWnd, message, wParam, lParam));
  305.      }
  306.      return (NULL);
  307. }
  308.  
  309.  
  310. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  311. HWND hDlg;                                /* window handle of the dialog box */
  312. unsigned message;                         /* type of message                 */
  313. WORD wParam;                              /* message-specific information    */
  314. LONG lParam;
  315. {
  316.      switch (message) {
  317.     case WM_INITDIALOG:           /* message: initialize dialog box */
  318.          return (TRUE);
  319.  
  320.     case WM_COMMAND:              /* message: received a command */
  321.          if (wParam == IDOK                /* "OK" box selected?         */
  322.                      || wParam == IDCANCEL) {      /* System menu close command? */
  323.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  324.         return (TRUE);
  325.          }
  326.          break;
  327.      }
  328.      return (FALSE);                  /* Didn't process a message    */
  329. }
  330.  
  331.  
  332. BOOL InitSSheetData()
  333. {
  334.     LOGFONT      lf ;
  335.  
  336.     // SSHEET defualt font CAN'T be a stock font because
  337.     // its deleted automaticaly by the control
  338.  
  339.     GetObject(GetStockObject(ANSI_VAR_FONT), sizeof(lf), &lf) ;
  340.     lf.lfItalic = TRUE ;
  341.  
  342.     SendMessage(hWndSSht, SS_SETDEFFONT,
  343.                         (WORD)CreateFontIndirect(&lf), NULL) ;
  344.  
  345.     SendMessage(hWndSSht, SS_SETCOLTITLE, 0, (LONG)"Member#") ;
  346.     SendMessage(hWndSSht, SS_SETCOLTITLE, 1, (LONG)"Home Church") ;
  347.     SendMessage(hWndSSht, SS_SETCOLTITLE, 2, (LONG)"Age") ;
  348.     SendMessage(hWndSSht, SS_SETCOLTITLE, 3, (LONG)"Position") ;
  349.     SendMessage(hWndSSht, SS_SETCOLTITLE, 4, (LONG)"Activity") ;
  350.     SendMessage(hWndSSht, SS_SETCOLTITLE, 5, (LONG)"Date Joined") ;
  351.     SendMessage(hWndSSht, SS_SETCOLTITLE, 6, (LONG)"Date Baptized") ;
  352.  
  353.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 0, MAKELONG(20,20+1)) ;
  354.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 1, MAKELONG(20,40+1)) ;
  355.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 2, MAKELONG(10,10+1)) ;
  356.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 3, MAKELONG(20,40+1)) ;
  357.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 4, MAKELONG(20,40+1)) ;
  358.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 5, MAKELONG(10,20+1)) ;
  359.     SendMessage(hWndSSht, SS_SETCOLWIDTH, 6, MAKELONG(10,20+1)) ;
  360.  
  361.     SendMessage(hWndSSht, SS_SETCOLJUST, 0, COLJUST_CENTER) ;
  362.     SendMessage(hWndSSht, SS_SETCOLJUST, 1, COLJUST_CENTER) ;
  363.     SendMessage(hWndSSht, SS_SETCOLJUST, 2, COLJUST_RIGHT) ;
  364.     SendMessage(hWndSSht, SS_SETCOLJUST, 3, COLJUST_CENTER) ;
  365.     SendMessage(hWndSSht, SS_SETCOLJUST, 4, COLJUST_CENTER) ;
  366.     SendMessage(hWndSSht, SS_SETCOLJUST, 5, COLJUST_LEFT) ;
  367.     SendMessage(hWndSSht, SS_SETCOLJUST, 6, COLJUST_LEFT) ;
  368.  
  369.     // initialize SSHEET data areas, TRUE if succeeded, else FALSE
  370.     return (SendMessage(hWndSSht, SS_INITSSHEET, NULL, NULL)) ;
  371. }
  372.  
  373. VOID CreateDummySSheetData(HWND hWndSSheet)
  374. {
  375.     int    i ;
  376.     char buffer[30] ;
  377.  
  378.     for (i=0; i <10; i++)
  379.         {
  380.         wsprintf (buffer, "%05d", i) ;
  381.         SendMessage(hWndSSheet, SS_SETCELLDATA, MAKEROWCOL(i,0), (LONG)buffer) ;
  382.         }
  383. }